<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="用户账号">
        <el-input v-model="queryParams.username" placeholder="用户账号" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="top-fun">
      <el-button type="success" @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="用户昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.note }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间	" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="	最后登录时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.loginTime }}
        </template>
      </el-table-column>
      <el-table-column label="	状态" align="center">
        <template slot-scope="scope">
          <!-- {{ scope.row.status==0?'禁用':'启用' }} -->
          <el-switch
            v-model="scope.row.statusB"
            @change="setStatus(scope.row)"
          />

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>

        </template>
      </el-table-column>

    </el-table>
    <div class="pagination">
      <el-pagination
        :current-page.sync="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
    <Add ref="add" />
  </div>
</template>

<script>
import Add from './add.vue'
import { adminUserList, adminUserEnable } from '@/api/table'
export default {
  components: {
    Add
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      queryParams: {

        mobile: '',
        pageSize: 10,
        pageNum: 1,
        nickName: '',
        username: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      adminUserList(this.queryParams).then(response => {
        response.data.records.map(item => {
          if (item.status === 1) {
            item.statusB = true
          } else {
            item.statusB = false
          }
          console.log(item.statusB)
        })
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },

    onSubmit() {
      this.queryParams.pageNum = 1
      this.fetchData()
    },
    reset() {
      this.queryParams.pageNum = 1
      this.queryParams.username = ''
      this.fetchData()
    },
    handleAdd() {
      this.$refs.add.show()
    },
    handleEdit(e) {
      this.$refs.add.edit(e)
    },
    async setStatus(user) {
      let status = ''
      if (user.status === 1) {
        status = 0
      } else {
        status = 1
      }
      await adminUserEnable({ id: user.id, status: status })
      this.fetchData()
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    }

  }
}
</script>
